package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Hugong;
import com.yl.auth.entity.HugongBean;
import com.yl.auth.model.entity.Yuangong;
import com.yl.auth.model.vo.YuangongQueryVo;
import com.yl.auth.service.HugongService;
import com.yl.auth.service.YuangongService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author
 * @datetime 2023/6/19 9:17
 * @description 类对象
 */
@RestController
@Api(tags = "基础设置-护工管理接口")
@RequestMapping("/admin/hugong/guanli")
public class    HugongController {
    @Resource
    private HugongService hugongService;
    @Resource
    private YuangongService yuangongService;
    //根据员工查询护工信息
    @ApiOperation(value = "根据员工查询护工信息")
    @GetMapping("/findAllhugong/{page}/{limit}")
    public Result<IPage> findAllhugong(@PathVariable @ApiParam("当前页") Integer page, @PathVariable @ApiParam("总条数") Integer limit,@ApiParam("员工实体类") Yuangong yuangong) {
        Page<Yuangong> page1 = new Page<>(page, limit);
        IPage<Yuangong> hugongBeanList = yuangongService.findAllhugong(page1, yuangong);
        return Result.ok(hugongBeanList);
    }
    //删除-------------------------------------------
     @ApiOperation(value = "护工删除")
    @DeleteMapping("remove/{id}")
    public Result delete(@PathVariable Integer id) {
        yuangongService.updateyuan(id);
        return Result.ok();
    }
    @ApiOperation(value = "删除时根据id查询护工老人数量返给前台一个count进行判断 1删除失败 0删除成功")
    @GetMapping("/getByIdHugong/{id}")
    public Result getByIdHugong(@PathVariable Integer id){
        Yuangong yuangong=yuangongService.getByIdhugong(id);
        return Result.ok(yuangong);
    }
    //全查员工表-------------------------------------------
    @ApiOperation(value = "全查员工表+模糊查询")
    @GetMapping("/findAllyuangong")
    public Result findAllyuangong(YuangongQueryVo yuangong){
        List<Yuangong> yuangonglist = yuangongService.findAll(yuangong);
        return Result.ok(yuangonglist);
    }
    //添加护工信息-------------------------------------------
    @ApiOperation(value = "添加护工")
    @PostMapping("/getyuangong")
    public Result getyuangong(@RequestBody List<Long> ids){
        for (Long id : ids) {
           yuangongService.updateyuangong(id);
        }
      return Result.ok();
    }


    /*图片上传上传图片接口*/
    /*图片上传地址*/
    String fileSavePath = "D:\\Vue2209java\\ylhomes\\public\\upload";
    /*格式化时间*/
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd/");
    @PostMapping("/saveUpload")
    @ApiOperation(value = "图片上传")
    @CrossOrigin
    public Result uploadFile (@ApiParam(name = "file", value = "上传", required = true) @RequestPart("file") MultipartFile file, HttpServletRequest request){
        /*将时间转化成字符串*/
        Date date = new Date();
        String directory = simpleDateFormat.format(date);
        /*文件路径是否存在
         * 如果不存在,则创建
         */
        File dir = new File(fileSavePath + directory);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        /*获取图片的名字*/
        String imageName = file.getOriginalFilename();
        System.out.println("图片上传位置" + fileSavePath + directory);
        /*给文件创建一个新的名字*/
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        /*图片新的名字*/
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
        System.out.println("上传的图片位置" + fileSavePath + directory + newFileName);
        /*创建新文件*/
        File newFile = new File(fileSavePath + directory + newFileName);
        try {
            file.transferTo(newFile);
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/images/" + directory + newFileName;
            System.out.println("图片上传成功的url:" + url);
            String sql = "/upload/" + directory + newFileName;
            /*返回给前端的字符串地址,然后添加的时候将这一串字符串加入到数据库中*/
            return Result.ok(sql);
        } catch (IOException e) {
            return Result.ok();
        }
    }
}
